<template>
  <a-modal
    v-model:visible="visible"
    :title="formData && formData.id ? '修改通知公告' :'新增通知公告'"
    draggable
    :width="width"
    @cancel="handleCancel"
  >
    <a-form :model="formData" ref="formRef" auto-label-width>
      <a-spin :loading="loading">
        <a-form-item
          label="标题"
          field="title"
          :rules="[{required:true, message:'公告标题不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-input v-model="formData.title" placeholder="请输入通知公告标题" allow-clear />
        </a-form-item>
        <a-form-item
          label="类型"
          field="type"
          :rules="[{required:true, message:'类型不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-select v-model="formData.type" placeholder="请选择类型">
            <a-option
              v-for="item in sys_notice_type"
              :key="item.value"
              :value="item.value"
            >{{ item.label }}</a-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="公告状态"
          field="status"
          :rules="[{required:true, message:'公告状态不能为空'}]"
          :validate-trigger="['blur']"
        >
          <Edit v-model="formData.content" v-if="visibleEdit" height="320px"></Edit>
        </a-form-item>
        <a-form-item
          label="公告状态"
          field="status"
          :rules="[{required:true, message:'公告状态不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-switch type="round" v-model="formData.status" :checked-value="1" :unchecked-value="0">
            <template #checked>{{ $t('searchTable.operation.enabled') }}</template>
            <template #unchecked>{{ $t('searchTable.operation.disabled') }}</template>
          </a-switch>
        </a-form-item>
      </a-spin>
    </a-form>
    <template #footer>
      <a-button
        style="margin-right: 8px"
        @click="handleCancel"
      >{{ $t('searchTable.columns.operations.close') }}</a-button>
      <a-button
        type="primary"
        @click="handleSubmit"
        :loading="loading"
      >{{ $t('searchTable.columns.operations.save') }}</a-button>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import { ref, watch, nextTick, getCurrentInstance } from 'vue';
import { useI18n } from 'vue-i18n';
import { Modal, Message } from '@arco-design/web-vue';
import {
  saveNotice,
  getNotice,
  updateNotice,
  NoticeData,
} from '@/api/sys/notice';

import Edit from '@/components/Edit/index.vue';

const { t } = useI18n();

// 初始化字典信息
const app = getCurrentInstance();
const { sys_notice_type } = app?.proxy?.useDict('sys_notice_type');

const loading = ref(false);
const visible = ref(false);
const visibleEdit = ref(false);
const width = ref('calc(38%)');

const formRef = ref();
const formData = ref<NoticeData>({
  status: 0,
  content: '',
});
const changed = ref(false);

const emit = defineEmits({ onSearch: null });

// 打开弹窗
const onOpen = (id: any) => {
  if (!id) {
    formData.value = {
      status: 0,
      content: '',
    };
    showForm();
    return;
  }
  getNotice(id).then((res: any) => {
    formData.value = res.data;
    formData.value.type = formData.value.type + '';
    showForm();
  });
};

const showForm = () => {
  visible.value = true;
  visibleEdit.value = true;
  nextTick(() => {
    changed.value = false;
  });
};

// 确认
const handleSubmit = async () => {
  formRef.value.validate().then((result: any) => {
    // 校验不通过
    if (result) {
      return;
    }
    loading.value = true;
    if (formData.value.id) {
      updateNotice(formData.value)
        .then(() => {
          onOk();
        })
        .finally(() => {
          loading.value = false;
        });
    } else {
      saveNotice(formData.value)
        .then(() => {
          onOk();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};

// 确认
const onOk = () => {
  formRef.value.clearValidate();
  formData.value = {};
  visible.value = false;
  visibleEdit.value = false;
  Message.success(t('operations.success'));
  emit('onSearch');
};

// 取消
const handleCancel = () => {
  if (!changed.value) {
    onCancel();
  } else {
    Modal.confirm({
      title: t('operations.cancel.title'),
      content: '',
      onOk() {
        onCancel();
      },
      onCancel() {
        visible.value = true;
      },
    });
  }
};

// 取消
const onCancel = () => {
  formRef.value.clearValidate();
  visible.value = false;
  visibleEdit.value = false;
};

// 抛出函数
defineExpose({
  onOpen,
});

// 监听数据
watch(
  formData,
  (newValue, oldValue) => {
    changed.value = true;
  },
  { deep: true }
);
</script>